package com.yingsheng.bi.hks.repository.dao.service;

import com.yingsheng.bi.hks.basic.utils.Utils;
import com.yingsheng.bi.hks.dto.serviceinfo.ServiceInfoQryReq;
import com.yingsheng.bi.hks.repository.dao.mapper.ServiceInfoMapper;
import com.yingsheng.bi.hks.repository.dao.mapper.ServiceOrdersMapper;
import com.yingsheng.bi.hks.repository.dao.po.ServiceInfo;
import com.yingsheng.bi.hks.repository.dao.po.ServiceInfoExample;
import com.yingsheng.bi.hks.repository.dao.po.ServiceOrders;
import com.yingsheng.bi.hks.repository.dao.po.ServiceOrdersExample;
import io.netty.util.internal.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import java.util.Date;
import java.util.List;

/**
 * ServiceInfo表的数据库操作服务
 * 要求说明：
 * 1.一般情况下,要求参数不超过5个时方法入参逐个声明
 * 2.一般情况下,要求条件不超过3个时要在方法名上体现
 * 3.最好对方法入参做一定校验,尤其是对条件参数至少要做非空校验
 *
 * @author 83686
 */
@Repository
public class ServiceInfoDaoService implements DaoService {

    @Autowired
    private ServiceInfoMapper serviceInfoMapper;

    @Autowired
    private ServiceOrdersMapper serviceOrdersMapper;

    public boolean insert(ServiceInfo serviceInfo) {
        serviceInfo.setId(Utils.genUUid());
        int result = serviceInfoMapper.insert(serviceInfo);
        return result == 1;
    }

    public boolean updateById(ServiceInfo serviceInfo) {
        serviceInfo.setUpdateTime(new Date());
        int result = serviceInfoMapper.updateByPrimaryKey(serviceInfo);
        return result == 1;
    }

    public List<ServiceInfo> query(ServiceInfoQryReq req) {
        ServiceInfoExample example = new ServiceInfoExample();
        ServiceInfoExample.Criteria criteria = example.createCriteria();
        if (!StringUtil.isNullOrEmpty(req.getCategoryId())) {
            criteria.andCategoryIdEqualTo(req.getCategoryId());
        }
        if (!StringUtil.isNullOrEmpty(req.getName())) {
            criteria.andNameLike("%" + req.getName() + "%");
        }
        example.setOrderByClause("type");
        return serviceInfoMapper.selectByExample(example);
    }

    public boolean del(String id) {
        return serviceInfoMapper.deleteByPrimaryKey(id) == 1;
    }

    public List<ServiceOrders> serverOrderListQry(ServiceOrders serviceOrders) {
        ServiceOrdersExample example = new ServiceOrdersExample();
        ServiceOrdersExample.Criteria criteria = example.createCriteria();
        if (!StringUtil.isNullOrEmpty(serviceOrders.getUserId())){
            criteria.andUserIdEqualTo(serviceOrders.getUserId());
        }
        if (!StringUtil.isNullOrEmpty(serviceOrders.getState())){
            criteria.andStateEqualTo(serviceOrders.getState());
        }
        return serviceOrdersMapper.selectByExample(example);
    }
}
